'use strict';
/**
 * Created by LeungZ on 2016/3/11.
 */
//------------------webpack build config----------------//
const contentPath = './app';
const sourcePath = './app/src';
const buildPath = './dist';
//const proxy = null;
// Set webpack dev server proxy
const proxy = {
    "/ctg-imageconsole-web/*": // API requests which matched will be proxied to backend server
    {//http://42.123.125.170:8083
        target: 'http://42.123.125.170:8083 ' // config backend server
        //target: 'http://10.4.231.123:7062' // config backend server
    }
};
//---------------webpack build config-----------------//

var webpack = require('webpack');
var path = require('path');
var autoprefixer = require('autoprefixer');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var isDev = (process.env.npm_lifecycle_event === 'dev') || (process.env.npm_lifecycle_event === 'server');

var config = {
    context: path.resolve(__dirname, contentPath),
    entry: {
        vendor: ['jquery', 'angular', 'echarts', 'js-yaml',
            "codemirror", 'codemirror/addon/lint/lint', 'codemirror/mode/yaml/yaml', 'codemirror/mode/dockerfile/dockerfile',
            'xterm', 'xterm/addons/fit/fit'],
        app: "./entry.js"
    },
    output: {
        path: path.resolve(__dirname, buildPath),
        filename: './js/' + (isDev ? "[name].js" : "[name].min.js")
    },
    module: {
        noParse: [/xterm.js/],
        loaders: [
            {
                test: require.resolve("jquery"),
                loader: 'expose?jQuery'
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'ng-annotate!babel?presets[]=es2015'
            },
            {
                test: /\.html$/,
                exclude: /index.html/,
                loader: 'ngtemplate?relativeTo=' + (path.resolve(__dirname, sourcePath)) + '/!html?attrs=false'
            },
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract("style", isDev ? "css?-url&sourceMap!postcss!sass?sourceMap" : "css?-url&minimize!postcss!sass")
            }
        ]
    },
    postcss: [autoprefixer({browsers: ['> 10%'], cascade: false})],
    plugins: [
        new CleanWebpackPlugin(buildPath),
        new CopyWebpackPlugin([
            {from: '*.min.css', to: 'css', context: 'pasp'},
            {from: '*.min.js', to: 'js', context: 'pasp'},
            {from: 'images', to: 'images'},
            {from: 'fonts', to: 'fonts'}
        ], {
            copyUnmodified: true
        }),
        new ExtractTextPlugin("css/" + (isDev ? "app.css" : "app.min.css")),
        new HtmlWebpackPlugin({
            isDev: isDev,
            chunks: ['app'],
            template: './index.html',
            minify: {
                removeComments: isDev ? false : true,
                collapseWhitespace: isDev ? false : true
            }
        }),
        new webpack.optimize.CommonsChunkPlugin("vendor", isDev ? './js/vendor.js' : './js/vendor.min.js'),
        new webpack.optimize.UglifyJsPlugin({
            output: {
                comments: false
            }
        })
    ]
};

if (process.env.npm_lifecycle_event === 'dev') {
    config.devtool = "cheap-inline-module-source-map";
    config.plugins.pop();
}
if (process.env.npm_lifecycle_event === 'server') {
    config.devtool = "source-map";
    config.plugins.shift();
    config.plugins.pop();
    config.devServer = {
        contentBase: path.resolve(__dirname, contentPath),
        port: '33001'
    };
    if (proxy) {
        config.devServer.proxy = proxy;
    }
}

module.exports = config;